import router from '@system.router'
import prompt from '@system.prompt'
import storage from '@system.storage'

export default {
  onCreate() {
    console.info('DetectiveApp onCreate')
    this.initializeApp()
  },

  onDestroy() {
    console.info('DetectiveApp onDestroy')
  },

  initializeApp() {
    // 初始化应用数据
    this.initCaseData()
    this.initPlayerData()
    this.initSettings()
  },

  initCaseData() {
    // 初始化案件数据
    const defaultCases = [
      {
        id: 1,
        title: "失踪的画家",
        difficulty: "简单",
        description: "一位知名画家神秘失踪，画室中发现可疑线索...",
        suspects: ["画廊老板", "助手", "竞争对手"],
        completed: false,
        unlocked: true
      },
      {
        id: 2,
        title: "午夜谋杀案",
        difficulty: "中等",
        description: "豪华酒店发生命案，每个人都有不在场证明...",
        suspects: ["酒店经理", "服务员", "受害者妻子", "商业伙伴"],
        completed: false,
        unlocked: false
      },
      {
        id: 3,
        title: "珠宝大盗",
        difficulty: "困难",
        description: "价值连城的珠宝被盗，安保系统完好无损...",
        suspects: ["保安", "清洁工", "珠宝商", "收藏家"],
        completed: false,
        unlocked: false
      }
    ]

    storage.get({
      key: 'cases',
      success: function(data) {
        if (!data) {
          storage.set({
            key: 'cases',
            value: JSON.stringify(defaultCases)
          })
        }
      },
      fail: function() {
        storage.set({
          key: 'cases',
          value: JSON.stringify(defaultCases)
        })
      }
    })
  },

  initPlayerData() {
    // 初始化玩家数据
    const defaultPlayerData = {
      level: 1,
      experience: 0,
      solvedCases: 0,
      successRate: 0,
      interrogationSkills: {
        psychology: 1,
        observation: 1,
        logic: 1
      },
      achievements: []
    }

    storage.get({
      key: 'playerData',
      success: function(data) {
        if (!data) {
          storage.set({
            key: 'playerData',
            value: JSON.stringify(defaultPlayerData)
          })
        }
      },
      fail: function() {
        storage.set({
          key: 'playerData',
          value: JSON.stringify(defaultPlayerData)
        })
      }
    })
  },

  initSettings() {
    // 初始化设置
    const defaultSettings = {
      voiceRecognition: true,
      sceneAwareness: true,
      multiDeviceSync: false,
      soundEffects: true,
      backgroundMusic: true
    }

    storage.get({
      key: 'settings',
      success: function(data) {
        if (!data) {
          storage.set({
            key: 'settings',
            value: JSON.stringify(defaultSettings)
          })
        }
      },
      fail: function() {
        storage.set({
          key: 'settings',
          value: JSON.stringify(defaultSettings)
        })
      }
    })
  }
}